package bsts.view.subscription;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import bsts.application.BSTSApplication;
import bsts.model.Account;
import bsts.model.Customer;
import bsts.model.Feature;
import bsts.model.Subscription;
import bsts.model.SubscriptionStatus;

public class UpdSubAddFeatureWindow extends JFrame {

	private static final long serialVersionUID = 1L;

	private BSTSApplication manager;
	private JFrame container;
	private JFrame me;
	
	private Subscription subscription;
	private Account account;
	private Customer customer;

	private AddFeaturePanel addFeaturePanel;
	private JPanel pnlButtons;

	private JButton btnSave;
	private JButton btnCancel;

	private ActionListener actlis_btnSave;
	private ActionListener actlis_btnCancel;

	private WindowListener windowListener = new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
			manager.getViewManager().goBackParent(container, me);
		}
	};

	public UpdSubAddFeatureWindow(BSTSApplication manager, JFrame container,
			ArrayList<Feature> availableFeatures,
			ArrayList<Feature> selectedFeatures, Subscription subscription, Account account, Customer customer) {
		super("Billing System for Telephone Services");

		this.manager = manager;
		this.container = container;
		this.me = this;
		
		this.subscription = subscription;
		this.account = account;
		this.customer = customer;

		addFeaturePanel = new AddFeaturePanel(availableFeatures,
				selectedFeatures);
		pnlButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT));

		btnSave = new JButton("Save");
		btnCancel = new JButton("Cancel");

		pnlButtons.add(btnSave);
		pnlButtons.add(btnCancel);

		this.setLayout(new BorderLayout());
		this.add(addFeaturePanel, BorderLayout.CENTER);
		this.add(pnlButtons, BorderLayout.SOUTH);

		this.addWindowListener(windowListener);

		initActionListener();
	}

	private void initActionListener() {
		actlis_btnSave = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (JOptionPane.showConfirmDialog(container,
						"Confirm to add selected feature?") == JOptionPane.YES_OPTION) {
					ArrayList<Feature> addedFeatures = addFeaturePanel
							.getAddedFeatures();
					ArrayList<Feature> selectedFeatures = addFeaturePanel
							.getSelectedFeatures();

					for (Feature feature : addedFeatures) {
						feature.setSubscriptionStatus(SubscriptionStatus.Activated);
						selectedFeatures.add(feature);
					}

					subscription.getPlan().setFeatures(selectedFeatures);
					account.addSubscription(subscription);
					customer.putAccount(account);
					manager.getCustomerManager().updateCustomer(customer);
					
					manager.getViewManager().goBackUpdSubscriptionWindow(container, me, account, customer);
				} else {
					manager.getViewManager().goBackParent(container, me);
				}
			}
		};

		actlis_btnCancel = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.getViewManager().goBackParent(container, me);
			}
		};

		btnSave.addActionListener(actlis_btnSave);
		btnCancel.addActionListener(actlis_btnCancel);
	}

}
